package com.ddone.util.mock;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author ddone
 * @date 2024/5/15-09:40
 */
public class DateFormatUtil {
    private static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    private static final DateTimeFormatter DTF_FULL = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static final DateTimeFormatter TF = DateTimeFormatter.ofPattern("HH:mm:ss");

    public static Date getDtWithCurTime(String dtStr) {
        LocalDateTime curTime = LocalDateTime.ofInstant((new Date()).toInstant(), ZoneId.systemDefault());
        String curTimeStr = TF.format(curTime);
        dtStr = dtStr + " " + curTimeStr;
        LocalDateTime theDateTime = LocalDateTime.parse(dtStr, DTF_FULL);
        return Date.from(theDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Long toTs(String dtStr, boolean isFull) {
        LocalDateTime localDateTime = null;
        if (!isFull) {
            dtStr = dtStr + " 00:00:00";
        }
        localDateTime = LocalDateTime.parse(dtStr, DTF);
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static Long toTs(String dtStr) {
        return toTs(dtStr, false);
    }

    public static String toDate(Long ts) {
        Date dt = new Date(ts);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(dt.toInstant(), ZoneId.systemDefault());
        return DTF.format(localDateTime);
    }

    public static String toYmdHms(Long ts) {
        Date dt = new Date(ts);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(dt.toInstant(), ZoneId.systemDefault());
        return DTF_FULL.format(localDateTime);
    }

    public static void main(String[] args) {
        System.out.println(toYmdHms(System.currentTimeMillis()));
    }
}
